package main

import (
	"context"
	"fmt"
	"github.com/micro/go-micro/transport/http"
	"github.com/micro/mdns"
	"log"

	"github.com/micro/go-micro"
	proto "micro-learning/proto/greeter"
)


func main() {
	// Create a new service. Optionally include some options here.
	service := micro.NewService(
		micro.Name("greeter.client"),
		micro.Transport(http.NewTransport()),
		)
	service.Init()

	// Create new greeter client
	greeter := proto.NewGreeterService("greeter", service.Client())

	// Call the greeter
	rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
	if err != nil {
		fmt.Println(err)
	}

	QueryService2("greeter")
	// Print response
	fmt.Println(rsp.Greeting)
}


func QueryService2(name string)  {
	params := mdns.DefaultParams(name)

	// 将双向chan赋值给输入chanel,并利用双向channel输出
	entries := make(chan *mdns.ServiceEntry)
	params.Entries = entries
	go func() {
		for item := range entries{
			fmt.Println(item)
		}
	}()


	if err := mdns.Query(params) ;err != nil {
		log.Panic(err)
	}

	<- params.Context.Done()
	fmt.Println("查询服务提供者结束")
}

